package com.zelin.seckill.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.SeckillGoodsEntity;
import com.zelin.seckill.service.SecKillService;
import com.zelin.utils.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/19-12:07
 * ------------------------------
 */
@Controller
public class SecKillController {

    @Reference(timeout = 5000)
    private SecKillService secKillService;

    /**
     * 功能: 根据当前时间列表秒杀商品
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/19 12:08
     */
    @RequestMapping({"/","seckill-index"})
    public String secKillList(Model model){
        //0. 得到日期菜单
        Date date = MyDate.getDateMenus().get(0);
        String dateStr = MyDate.getDateStr(date);
        //1. 得到所有商品(指定日期空间下的某些商品)
        List<SeckillGoodsEntity> seckillGoodsEntities = secKillService.findSeckillGoods(dateStr);
        //2. 得到以当前时间小时数为起点的往向5个时间范围
        List<Date> menus = secKillService.listMenus();
        //3. 将上面的两组数据放到model中
        model.addAttribute("seckillGoods",seckillGoodsEntities);
        model.addAttribute("menus",menus);
        return "seckill-index";
    }

    /**
     * 功能: 展示菜单
     * 参数:
     * 返回值:
     * 时间: 2021/8/19 13:11
     */
    @RequestMapping("menus")
    public String listMenus(Model model){
        List<Date> menus = secKillService.listMenus();
        model.addAttribute("menus",menus);
        return "seckill-index";
    }

    /**
     * 功能: 根据秒杀商品id查询秒杀商品
     * 参数:
     * 返回值:
     * 时间: 2021/8/19 14:49
     */
    @RequestMapping("findSeckillGoods")
    public String findSeckillGoods(String seckillId,Model model){
        Date date = MyDate.getDateMenus().get(0);
        String dateStr = MyDate.getDateStr(date);
        //1. 在redis中根据secKillId查询秒杀商品
        SeckillGoodsEntity seckillGoodsEntity = secKillService.findSecKillGoodsById(seckillId,dateStr);
        //2. 放到model 中
        model.addAttribute("seckillGoods",seckillGoodsEntity);
        //3. 返回到seckill-item.html
        return "seckill-item";
    }


}
